package com.login;

import javax.security.auth.login.LoginContext;

import com.menu.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	private String uname;
	private String upass;
	private String role;
	
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getUpass() {
		return upass;
	}
	public void setUpass(String upass) {
		this.upass = upass;
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("Returning to "+role);
		ActionContext.getContext().getSession().put("username", uname);
		ActionContext.getContext().getSession().put("userrole", role);
		if(role.equalsIgnoreCase("null"))
			role = Constants.UNAUTHORISED;
		return role;
	}
	
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		System.out.println("In validate");
		LoginFunctions loginFunctions = new LoginFunctions();
		role = loginFunctions.whatIsRole(getUname(), getUpass());
		if (role==null || "".equals(role.trim())) {
			
			addActionError("Username//Password not matching");
			addFieldError("uname", "username password not found");
		}
	}
}
